﻿using System;
using System.Text;
using System.Drawing;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static partial class NativeAOT
{
    [UnmanagedCallersOnly(EntryPoint = "runge_kutta")]
    public static unsafe void runge_kutta(double t, double h, int n, IntPtr y_ptr, double eps, IntPtr f_x_ya_n_da_ptr)
    {
        double* y = (double*)y_ptr.ToPointer();
        f_x_ya_n_da = Marshal.GetDelegateForFunctionPointer<delegatefunc_x_ya_n_da>(f_x_ya_n_da_ptr);

        runge_kutta(t, h, n, y, eps);
    }

    /// <summary>
    /// 变步长Runge_Kutta方法
    /// f计算微分方程组中各方程右端函数值的函数名。
    /// </summary>
    /// <param name="t">积分起始点。</param>
    /// <param name="h">积分步长。</param>
    /// <param name="n">一阶微分方程组中方程个数，也是未知函数个数。</param>
    /// <param name="y">y[n]存放n个未知函数在起始点t处的函数值。返回n个未知函数在t+h处的函数值。</param>
    /// <param name="eps">控制精度要求。</param>
    public static unsafe void runge_kutta(double t, double h, int n, double* y, double eps)
    {
        int m, i, j, k;
        double hh, p, dt, x, tt, q;
        double* a = stackalloc double[4];
        double* g = stackalloc double[n];
        double* b = stackalloc double[n];
        double* c = stackalloc double[n];
        double* d = stackalloc double[n];
        double* e = stackalloc double[n];

        hh = h;
        m = 1;
        p = 1.0 + eps;
        x = t;
        for (i = 0; i <= n - 1; i++)
        {
            c[i] = y[i];
        }
        while (p >= eps)
        {
            a[0] = hh / 2.0;
            a[1] = a[0];
            a[2] = hh;
            a[3] = hh;
            for (i = 0; i <= n - 1; i++)
            {
                g[i] = y[i]; y[i] = c[i];
            }
            dt = h / m;
            t = x;
            for (j = 0; j <= m - 1; j++)
            {
                f_x_ya_n_da(t, y, n, d);
                for (i = 0; i <= n - 1; i++)
                {
                    b[i] = y[i];
                    e[i] = y[i];
                }
                for (k = 0; k <= 2; k++)
                {
                    for (i = 0; i <= n - 1; i++)
                    {
                        y[i] = e[i] + a[k] * d[i];
                        b[i] = b[i] + a[k + 1] * d[i] / 3.0;
                    }
                    tt = t + a[k];
                    f_x_ya_n_da(tt, y, n, d);
                }
                for (i = 0; i <= n - 1; i++)
                {
                    y[i] = b[i] + hh * d[i] / 6.0;
                }
                t = t + dt;
            }
            p = 0.0;
            for (i = 0; i <= n - 1; i++)
            {
                q = Math.Abs(y[i] - g[i]);
                if (q > p) p = q;
            }
            hh = hh / 2.0;
            m = m + m;
        }
        return;
    }

    /*
    // 变步长Runge_Kutta方法例
      int main()
      { 
          int i, j;
          void  rktf(double,double [],int,double []);
          double t,h,eps,y[2];
          y[0]=0.0; y[1]=1.0;
          t=0.0; h=0.1; eps=0.0000001;
          cout <<"t = " <<t;
          for (i=0; i<=1; i++)
              cout <<"  y(" <<i <<") = " <<setw(10) <<y[i];
          cout <<endl;
          for (j=1; j<=10; j++)
          { 
              runge_kutta(t,h,2,y,eps,rktf);
              t=t+h;
              cout <<"t = " <<t;
              for (i=0; i<=1; i++)
                  cout <<"  y(" <<i <<") = " <<setw(10) <<y[i];
              cout <<endl;
          }
          return 0;
      }
    // 计算微分方程组中各方程右端函数值
      void rktf(double t, double y[], int n, double d[])
      { 
          t=t; n=n;
          d[0]=y[1]; d[1]=-y[0];
          return;
      }
    */
}

